// ActionScript file

import flash.events.KeyboardEvent;
import flash.ui.Keyboard;

import mx.controls.Alert;
import mx.managers.PopUpManager;

import org.flexlancer.Logger.Logger;
import org.flexlancer.databases.dbServices;

private var serviceName:String;

[Bindable]
private var priceInfo:Array = new Array();

private var editObject:Object = null;

public function set EditObject(val:Object):void{
	try{
		this.editObject = val;
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnCreationComplete():void{
	try{
		if ( this.editObject != null ){
			this.txtServiceDescription.text = this.editObject.servicename;
			this.priceInfo = this.editObject.prices;
			
			for each ( var singlePrice:Object in this.priceInfo ){
				singlePrice.type = singlePrice.pricetype;
			}
			
			this.dgPriceInfo.dataProvider = this.priceInfo;
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnAddPrice():void{
	try{
			//Alert.show(this.rgContactTypeGroup.selectedValue.toString());
			var info:Object = new Object();
			var a:Boolean,b:Boolean = false;
			
			if(this.txtPriceType.text.length > 0){
				info.type = this.txtPriceType.text;
				a=true;
				//this.priceInfo.push(info);
			}else	{
				Alert.show("Details are missing!","Field Empty");
			}
			
			if(this.txtPrice.text.length > 0){
				info.price = this.txtPrice.text;
				b=true;
				//this.priceInfo.push(info);
			}else	{
				Alert.show("Details are missing!","Field Empty");
			}
			if(a && b){
				this.priceInfo.push(info);
			}
			this.dgPriceInfo.dataProvider = this.priceInfo;
			this.txtPriceType.text = "";
			this.txtPrice.text = "";
			this.txtPriceType.setFocus();

		
			//Alert.show(this.custInfo.toString());
	}catch(err:Error){
		Logger.LogE(err);
	}
}

private function OnSaveClick():void{
	try{
		if(this.txtServiceDescription.text.length<1){
			Alert.show("Service Description Field is Empty","Empty Field");
			return;
		}else
		{
			this.serviceName = this.txtServiceDescription.text;
		}
		
		if ( this.priceInfo.length == 0 ) {
			Alert.show("Atleast 1 service price must be defined","Service without price");
			return;
		}
		if ( this.editObject == null ){
			dbServices.AddService(this.serviceName, this.priceInfo);
	
			if(this.chkAddAnother.selected){
				this.txtServiceDescription.text = "";
				this.priceInfo = new Array();
				this.txtServiceDescription.setFocus();
			}else{
				PopUpManager.removePopUp(this);
			}	
		}else{
			dbServices.Update(this.editObject.serviceid,this.serviceName,this.priceInfo);
			PopUpManager.removePopUp(this);
		}		
			
	}catch(err:Error){
		Logger.LogE(err);
	}
}
override protected function OnKeyUp(evt:KeyboardEvent):void{
	try{
		super.OnKeyUp(evt);
		if ( evt.keyCode == Keyboard.DELETE ) {
			this.OnDeletePrice();
		} 
	}catch(err:Error){
		Logger.LogE(err);
	}
}
private function OnDeletePrice():void{
	try{
		if ( this.dgPriceInfo.selectedItem != null ){
			var index:Number = this.priceInfo.indexOf(this.dgPriceInfo.selectedItem);
			this.priceInfo.splice(index,1);
			this.dgPriceInfo.dataProvider = this.priceInfo;
		}
	}catch(err:Error){
		Logger.LogE(err);
	}
}

